﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;

namespace TPMS_ScanComparison.Control
{
    public partial class SetControl : UserControl
    {
        Form form;
        public SetControl(Form _form)
        {
            form = _form;
            InitializeComponent();
            Load += SetControl_Load;
        }

        private void SetControl_Load(object sender, EventArgs e)
        {
            string[] ArryPort = SerialPort.GetPortNames();
            cb_plc_port.Items.Clear();
            cb_Product_port.Items.Clear();
            cb_读码器_port.Items.Clear();
            cb_Power_port.Items.Clear();
            for (int i = 0; i < ArryPort.Length; i++)
            {
                cb_plc_port.Items.Add(ArryPort[i]);
                cb_读码器_port.Items.Add(ArryPort[i]);
                cb_Product_port.Items.Add(ArryPort[i]);
                cb_Power_port.Items.Add(ArryPort[i]);
            }
            cb_plc_port.Text = Global.PLC.sPort;
            tb_plc_rate.Text = Global.PLC.iRate.ToString();
            tb_plc_databit.Text = Global.PLC.iDatabit.ToString();
            tb_plc_stopbit.Text = Global.PLC.sStopbit.ToString();
            cb_plc_parity.Text = Global.PLC.sParity;

            cb_Product_port.Text = Global.产品.sPort;
            tb_Product_rate.Text = Global.产品.iRate.ToString();
            tb_Product_databit.Text = Global.产品.iDatabit.ToString();
            tb_Product_stopbit.Text = Global.产品.sStopbit.ToString();
            cb_Product_parity.Text = Global.产品.sParity;

            cb_读码器_port.Text = Global.读码.sPort;
            tb_读码器_rate.Text = Global.读码.iRate.ToString();
            tb_读码器_databit.Text = Global.读码.iDatabit.ToString();
            tb_读码器_stopbit.Text = Global.读码.sStopbit.ToString();
            cb_读码器_parity.Text = Global.读码.sParity;

            cb_Power_port.Text = Global.电源.sPort;
            tb_Power_rate.Text = Global.电源.iRate.ToString();
            tb_Power_databit.Text = Global.电源.iDatabit.ToString();
            tb_Power_stopbit.Text = Global.电源.sStopbit.ToString();
            cb_Power_parity.Text = Global.电源.sParity;

            if (Global.PLC.serial.sp.IsOpen)
            {
                btn_plc_Open_port.Enabled = false;
                btn_plc_Close_port.Enabled = true;
                //panelPLC.Enabled = false;
            }
            else
            {
                btn_plc_Open_port.Enabled = true;
                btn_plc_Close_port.Enabled = false;
                panelPLC.Enabled = true;
            }

            if (Global.电源.serial.sp.IsOpen)
            {
                btn_电源_Open_port.Enabled = false;
                btn_电源_Close_port.Enabled = true;
                // panel电源.Enabled = false;
            }
            else
            {
                btn_电源_Open_port.Enabled = true;
                btn_电源_Close_port.Enabled = false;
                panel电源.Enabled = true;
            }


            if (Global.产品.serial.sp.IsOpen)
            {
                btn_product_open_port.Enabled = false;
                btn_product_close_port.Enabled = true;
                // panel产品.Enabled = false;
            }
            else
            {
                btn_product_open_port.Enabled = true;
                btn_product_close_port.Enabled = false;
                panel产品.Enabled = true;
            }

            if (Global.读码.serial.sp.IsOpen)
            {
                btn_读码器_Open.Enabled = false;
                btn_读码器_Close.Enabled = true;
                // panel扫码.Enabled = false;
            }
            else
            {
                btn_读码器_Open.Enabled = true;
                btn_读码器_Close.Enabled = false;
                panel扫码.Enabled = true;
            }

        }

        private void PLC赋值()
        {
            Global.PLC.sPort = cb_plc_port.Text;
            Global.PLC.iRate = int.Parse(tb_plc_rate.Text);
            Global.PLC.iDatabit = int.Parse(tb_plc_databit.Text);
            Global.PLC.sStopbit = tb_plc_stopbit.Text;
            Global.PLC.sParity = cb_plc_parity.Text;
        }

        private void 电源赋值()
        {
            Global.电源.sPort = cb_Power_port.Text;
            Global.电源.iRate = int.Parse(tb_Power_rate.Text);
            Global.电源.iDatabit = int.Parse(tb_Power_databit.Text);
            Global.电源.sStopbit = tb_Power_stopbit.Text;
            Global.电源.sParity = cb_Power_parity.Text;
        }

        private void Product赋值()
        {
            Global.产品.sPort = cb_Product_port.Text;
            Global.产品.iRate = int.Parse(tb_Product_rate.Text);
            Global.产品.iDatabit = int.Parse(tb_Product_databit.Text);
            Global.产品.sStopbit = tb_Product_stopbit.Text;
            Global.产品.sParity = cb_Product_parity.Text;
        }

        private void 读码器赋值()
        {
            Global.读码.sPort = cb_读码器_port.Text;
            Global.读码.iRate = int.Parse(tb_读码器_rate.Text);
            Global.读码.iDatabit = int.Parse(tb_读码器_databit.Text);
            Global.读码.sStopbit = tb_读码器_stopbit.Text;
            Global.读码.sParity = cb_读码器_parity.Text;
        }

        private void btn_电源_Open_port_Click(object sender, EventArgs e)
        {

            电源赋值();
            Global.电源.参数赋值(Global.电源);

            Global.WritePowPara();
            if (!Global.电源.serial.Broad_PortInitialize())
            {
                AntdUI.Message.error(form, "开启失败", Font);
                return;
            }
            AntdUI.Message.success(form, "开启成功", Font);

            btn_电源_Close_port.Enabled = true;
            btn_电源_Open_port.Enabled = false;
           // panel电源.Enabled = false;
        }

        private void btn_电源_Close_port_Click(object sender, EventArgs e)
        {
            Global.电源.serial.sp.Close();
            btn_电源_Close_port.Enabled = false;
            btn_电源_Open_port.Enabled = true;
            panel电源.Enabled = true;
        }

        private void btn_电源_save_Click(object sender, EventArgs e)
        {
            电源赋值();
            Global.WritePowPara();
            AntdUI.Message.success(form, "保存成功", Font);

        }

        private void btn_plc_Open_port_Click(object sender, EventArgs e)
        {

            PLC赋值();
            Global.PLC.参数赋值(Global.PLC);
            //      global.plc参数赋值();
            if (!Global.PLC.serial.Broad_PortInitialize())
            {
                AntdUI.Message.error(form, "开启失败", Font);
                return;
            }
            AntdUI.Message.success(form, "开启成功", Font);

            btn_plc_Close_port.Enabled = true;
            btn_plc_Open_port.Enabled = false;
           // panelPLC.Enabled = false;
        }

        private void btn_plc_Close_port_Click(object sender, EventArgs e)
        {
            Global.PLC.serial.sp.Close();

            btn_plc_Close_port.Enabled = false;
            btn_plc_Open_port.Enabled = true;
            panelPLC.Enabled = true;
        }

        private void btn_plc_save_Click(object sender, EventArgs e)
        {
            PLC赋值();
            Global.WriteplcPara();
            AntdUI.Message.success(form, "保存成功", Font);
        }

        private void btn_product_open_port_Click(object sender, EventArgs e)
        {

            Product赋值();
            Global.产品.参数赋值(Global.产品);
            //       global.Pro参数赋值();
            if (!Global.产品.serial.Broad_PortInitialize())

            {
                AntdUI.Message.error(form, "开启失败", Font);
                return;
            }
            AntdUI.Message.success(form, "开启成功", Font);

            btn_product_close_port.Enabled = true;
            btn_product_open_port.Enabled = false;
            //panel产品.Enabled = false;
        }

        private void btn_product_close_port_Click(object sender, EventArgs e)
        {
            Global.产品.serial.sp.Close();
            btn_product_close_port.Enabled = false;
            btn_product_open_port.Enabled = true;
            panel产品.Enabled = true;
        }

        private void btn_product_save_Click(object sender, EventArgs e)
        {
            Product赋值();
            Global.WriteProductPara();
            AntdUI.Message.success(form, "保存成功", Font);

        }

        private void btn_读码器_Open_Click(object sender, EventArgs e)
        {

            读码器赋值();
            Global.读码.参数赋值(Global.读码);
            //      global.读码器参数赋值();
            if (!Global.读码.serial.Broad_PortInitialize())
            {
                AntdUI.Message.error(form, "开启失败", Font);
                return;
            }
            AntdUI.Message.success(form, "开启成功", Font);

            btn_读码器_Open.Enabled = false;
            btn_读码器_Close.Enabled = true;
            //panel扫码.Enabled = false;
        }

        private void btn_读码器_Close_Click(object sender, EventArgs e)
        {
            Global.读码.serial.sp.Close();
            btn_读码器_Open.Enabled = true;
            btn_读码器_Close.Enabled = false;
            panel扫码.Enabled = true;
        }

        private void btn_读码器_save_Click(object sender, EventArgs e)
        {
            读码器赋值();
            Global.Write读码器Para();
            AntdUI.Message.success(form, "保存成功", Font);
        }
        // 通用的端口打开事件处理方法


    }
}
